/**
 * 
 */
package com.acme.bankapp.domain.bank;

import com.acme.bankapp.exception.NotEnoughFundsException;

/**
 * @author Labun Konstantin
 * 
 */
public abstract class AbstractAccount implements Account {

    protected double balance;

    protected AbstractAccount(double startBalance) {
	super();
	if (balance < 0) {
	    throw new IllegalArgumentException();
	}
	balance = startBalance;
    }

    @Override
    public void deposit(double x) {
	balance += x;
    }

    /**
     * @return the balance
     */
    @Override
    public double getBalance() {
	return balance;
    }

    @Override
    public double maximumAmountToWithdraw() {
	return balance;
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
	return "Account's balance = " + getBalance();
    }

    @Override
    public void withdraw(double x) throws NotEnoughFundsException {
	if (maximumAmountToWithdraw() <= x) {
	    throw new NotEnoughFundsException("Not enough funds!");
	}
	if (balance >= x) {
	    balance -= x;
	}
    }

    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	long temp;
	temp = Double.doubleToLongBits(balance);
	result = prime * result + (int) (temp ^ (temp >>> 32));
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	if (getClass() != obj.getClass())
	    return false;
	AbstractAccount other = (AbstractAccount) obj;
	if (Double.doubleToLongBits(balance) != Double
		.doubleToLongBits(other.balance))
	    return false;
	return true;
    }
    
    
}
